<?php
/*
 * 插件监听管理类
 * @package ob
 * @subpackage libraries
 * @category libraries
 * @author zhouwei learned from Saturn
 *
 *
 */
 class plugin{
 	/*
 	 *  监听已注册的插件。
 	 * @access private
 	 * @var array
 	 */
 	private $_listeners = array();

 	private $_CI;
 	/*
 	 * 构造函数,装载已激活的插件
 	 * @access private
 	 * @return void
 	 */
	public function __construct(){

		$this->_CI = & get_instance();

		$plugins = unserialize(common::_get_setting('active_plugins'));

		if($plugins && is_array($plugins))
		{
			foreach($plugins as $plugin)
			{
				$plugin_dir =$plugin['directory'].'/'.ucfirst($plugin['directory']).'.php';

			 	$file = FCPATH.OB_PLUGINS_DIR.'/'.$plugin_dir;


				if(preg_match("/^[\w\-\_]+\/[\w\_\-]+\.php$/",$plugin_dir) && file_exists($file))
				{
					include_once($file);

					$class = ucfirst($plugin['directory']);

					if(class_exists($class))
					{
						new $class($this);
					}
				}else{

				}
			}
		}
	}

	/*
	 * 注册进$_listeners
	 *
	 */
	public function registe($name,&$reference,$method){

		$key = get_class($reference).'->'.$method;

		$this->_listeners[$name][$key] = array(&$reference,$method);



		log_message('debug','注册'.$name);
	}

	/*
	 * 调用钩子
	 *
	 */
	public function trigger($name){

		if(isset($this->_listeners[$name]) &&  is_array($this->_listeners[$name])){

			foreach($this->_listeners[$name] as $listener){

				$class = &$listener[0];

				$method = $listener[1];

				if(method_exists($class,$method)){

					$args = array_slice(func_get_args(),1);
				}

				call_user_func_array(array($class,$method),$args);
			}

		}
	}
 }
?>
